package servlet;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entity.Appointment;
import entity.User;
import session.AppointmentDao;

 
/**
 * Kontroler koji omogućuje brisanje termina
 * @author rigo & jung
 * 
 */
public class AppDelete extends HttpServlet {
	private static final long serialVersionUID = -5928731605482687316L;
	
	
	@EJB
	protected AppointmentDao appointmentDao;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try{
			User admin;
			if((admin = (User)request.getSession().getAttribute("user"))==null ||
					admin.getType()!='A'){
				response.sendRedirect(response.encodeURL("./login.jsp"));
				return;
			}
			
			if( request.getParameter("appId")!=null){
				Appointment app = appointmentDao.findById(Integer.parseInt(request.getParameter("appId")));	
				app.setDeleted(true);
				appointmentDao.merge(app);
			}
			 
			getServletContext().getRequestDispatcher("/ListAppointments").forward(request, response);
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}
